package com.lai.note.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.gson.Gson;
import com.lai.note.R;
import com.lai.note.dto.ResponseResult;
import com.lai.note.dto.UserDto;
import com.lai.note.model.User;
import com.lai.note.service.UserBaseService;
import com.lai.note.util.GsonUtil;
import com.lai.note.util.RetrofitRequest;
import com.lai.note.util.ToastUtil;
import com.lai.note.util.UserBaseUtil;

import java.io.IOException;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class PersonDetailsActivity extends AppCompatActivity {

    //用户相关信息及控件
    private String userId;
    private EditText et_username;
    private EditText et_sex;
    private EditText et_age;
    private EditText et_birthday;
    private EditText et_phone;
    private EditText et_email;
    private EditText et_address;
    private EditText et_signature;
    private String iconPath;
    private Button btn_update;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_details);
        Log.d("main","-->PersonDetailsActivity创建");
        initViews();
        initUserInfo();
        btn_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserBaseUtil.updateUserInfo(getApplicationContext(),userModify2Save());
                finish();
                overridePendingTransition(0,0);
            }
        });
    }


    private void initViews(){
        et_username = findViewById(R.id.et_username);
        et_sex = findViewById(R.id.et_sex);
        et_age = findViewById(R.id.et_age);
        et_birthday = findViewById(R.id.et_birth);
        et_phone = findViewById(R.id.et_phone);
        et_email = findViewById(R.id.et_email);
        et_address = findViewById(R.id.et_address);
        et_signature = findViewById(R.id.et_signature);
        btn_update = findViewById(R.id.btn_updateUser);
    }

    private void initUserInfo() {
        Retrofit retrofit = RetrofitRequest.getInstance(this);
        UserBaseService userBaseService = retrofit.create(UserBaseService.class);
        Call<ResponseResult> call = userBaseService.getUserInfo();
        call.enqueue(new Callback<ResponseResult>() {
            @Override
            public void onResponse(Call<ResponseResult> call, Response<ResponseResult> response) {
                ResponseResult res = response.body();
                if (res != null && res.getStatus() == 200) {
                    String userJson = GsonUtil.object2Json(res.getData());
                    User user = new Gson().fromJson(userJson, User.class);
                    setUserInfos(user);
                } else {
                    ToastUtil.display(getApplicationContext(), "加载用户信息失败-_--_-");
                }
            }

            @Override
            public void onFailure(Call<ResponseResult> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

    private void setUserInfos(User user){
        System.out.println("user:"+user);
        userId = user.getUserId();
        et_username.setText(user.getUserName());
        et_sex.setText(user.getSex()?"男":"女");
        et_age.setText(String.valueOf(user.getAge()));
        et_birthday.setText(user.getBirthday());
        et_phone.setText(user.getPhone());
        et_email.setText(user.getEmail());
        et_address.setText(user.getAddress());
        et_signature.setText(user.getSignature());
        iconPath = user.getIconPath();
    }

    //用户修改信息后，将这些信息封装成json后发送到后台，保存到数据库
    private String userModify2Save(){
        UserDto userDto = new UserDto();
        userDto.setUserId(userId);
        userDto.setUserName(et_username.getText().toString());
        userDto.setSex(et_sex.getText().toString());
        userDto.setPhone(et_phone.getText().toString());
        userDto.setEmail(et_email.getText().toString());
        userDto.setAge(Integer.parseInt(et_age.getText().toString()));
        userDto.setBirthday(et_birthday.getText().toString());
        userDto.setAddress(et_address.getText().toString());
        userDto.setSignature(et_signature.getText().toString());
        userDto.setIconPath(iconPath);
        String userDtoJson = GsonUtil.object2Json(userDto);
        System.out.println("userDtoJson:-->" + userDtoJson);
        return userDtoJson;
    }

}
